package com.customer.service.driver;
import com.customer.mapper.DriverMapper;
import com.customer.model.Driver;
import com.customer.util.ResponseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverMapper driverMapper;


    @Override
    public ResponseData findAllByPage(int page, int limit, String phoneNumber) {
        // 分页查询方法
        // 开启分页 两个参数  1从第几页开始  2.展示几条  后一行必须跟查询语句
        PageHelper.startPage(page, limit);
        // 执行查询语句
        List<Driver> drivers = driverMapper.findAll(phoneNumber);
        PageInfo pageInfo = new PageInfo<>(drivers);
        return ResponseData.success(pageInfo);
    }

    @Override
    public ResponseData updateShow(int id) {
        Driver driver = driverMapper.selectOneById(id);
        return ResponseData.success(driver);
    }


    @Override
    public ResponseData update(Driver driver) {
        int count = driverMapper.update(driver);
        if (count == 1) {
            return ResponseData.success();
        }
        return ResponseData.fail("修改失败");
    }

    @Override
    public ResponseData deleteDriver(int id) {
        int count = driverMapper.deleteOneById(id);
        if (count > 0) {
            return ResponseData.success();
        }
        return ResponseData.fail("删除失败，请重新操作!");
    }
}
